package com.ego.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ego.admin.dao.SysRoleDao;
import com.ego.admin.po.SysRole;
import com.ego.admin.service.SysAdminRoleService;
import com.ego.admin.service.SysAdminService;
import com.ego.admin.service.SysRoleMenuService;
import com.ego.admin.service.SysRoleService;
import com.ego.admin.util.RoleUtil;
import com.ego.admin.vo.SysRoleSearchVo;
import com.ego.common.constant.SystemConstant;
import com.ego.common.exception.GlobalException;
import com.ego.common.util.PageUtil;
import com.ego.common.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @program: ego
 * @description: 实现类
 * @author: KamTang
 * @create: 2023-02-22 19:14
 **/
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRole> implements SysRoleService {

    @Autowired
    private SysRoleMenuService roleMenuService;
    @Autowired
    private SysAdminService adminService;
    @Autowired
    private SysAdminRoleService adminRoleService;


    @Override
    public List<Long> queryByUserId(Long userId) {
        return this.baseMapper.get(userId);
    }

    @Override
    public PageVo<SysRole> search(SysRoleSearchVo search) {
        LambdaQueryWrapper<SysRole> query = Wrappers.<SysRole>lambdaQuery()
                .eq(search.getCreateUserId() != null, SysRole::getCreateUserId, search.getCreateUserId());
        if (StrUtil.isNotEmpty(search.getRoleName())) {
            query.like(SysRole::getRoleName, search.getRoleName());
        }
        Page<SysRole> page = baseMapper.selectPage(Page.of(search.getPageNum(), search.getPageSize()), query);
        return PageUtil.toPage(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRole(SysRole role) {
        this.save(role);
        // 检查是否越权
        boolean checkPerms = RoleUtil.checkPerms(role, adminService.getMenuIdByUserId(role.getCreateUserId()));
        if (!checkPerms) {
            throw new GlobalException("新增角色的权限，已超出你的权限范围");
        }
        // 保存角色与菜单的关系
        roleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysRole role) {
        this.updateById(role);
        boolean checkPerms = RoleUtil.checkPerms(role, adminService.getMenuIdByUserId(role.getCreateUserId()));
        if (!checkPerms) {
            throw new GlobalException("新增角色的权限，已超出你的权限范围");
        }

        //更新角色与菜单关系
        roleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] roleIds) {
        // 删除角色
        this.removeByIds(Arrays.asList(roleIds));

        // 删除角色与菜单关联
        roleMenuService.removeByIds(Arrays.asList(roleIds));

        // 删除角色与用户关联
        adminRoleService.removeByIds(Arrays.asList(roleIds));
    }

    @Override
    public SysRole getRoleInfoById(Long id) {
        SysRole roleEntity = this.baseMapper.selectById(id);
        List<Long> menuIdList = roleMenuService.queryMenuIdList(id);
        roleEntity.setMenuIdList(menuIdList);
        return roleEntity;
    }

    @Override
    public List<SysRole> getRoleInfoByUserId(Long userId) {
        return this.baseMapper.gets(userId);
    }


}
